import 'package:flutter/cupertino.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';

import '../../widgets/index.dart';

abstract class BaseStateV<T extends StatefulWidget> extends State<T>
    with
        HzyScaffolMixin,
        HzyAppBarMixin,
        HzyBodyMixin,
        HzyAbsAttribute,
        HzyAbstractNetWork,
        HzyNormalLifeCycleAbs,
        WidgetsBindingObserver {
  /// 配置缺省页状态值
  CommonPlaceHoldType placeHoldType = CommonPlaceHoldType.emptyData;
  String? errMsg;
  @override
  bool get safeAreaBottom => false;
  bool isShowLoading = false;
  PageState pageState = PageState.initializedState;

  /// 界面初始化完成
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      interfaceRenderingCompleted();
    });
    WidgetsBinding.instance.addObserver(this);
    super.initState();
    configDefault();
  }

  configDefault() {}
  @override
  void didChangeAppLifecycleState(
    AppLifecycleState state,
  ) {
    if (state == AppLifecycleState.resumed) {
      debugLog("app进入前台");
      configAppLifeCycleResumed();
    } else if (state == AppLifecycleState.inactive) {
      debugLog("app在前台但不响应事件，比如电话，touch id等");
      configAppLifeCycleInactive();
    } else if (state == AppLifecycleState.paused) {
      debugLog("app进入后台");
      configAppLifeCyclePaused();
    } else if (state == AppLifecycleState.detached) {
      debugLog("没有宿主视图但是flutter引擎仍然有效");
      configAppLifeCycleDetached();
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(
    BuildContext context,
  ) {
    return createBuild(context: context);
  }

  @override
  Widget createBuild({
    required BuildContext context,
  }) {
    return createScaffolWidget(context: context);
  }

  /// 是否显示加载动画
  @override
  bool configIsshowLoading() {
    return isShowLoading;
  }

  @override
  Widget? createLoadingWidget() {
    return configLoadWidget();
  }

  /// 创建缺省页
  @override
  Widget? createEmptyWidget() {
    return PlaceHoldPage(
      placeHoldType: placeHoldType,
      msg: errMsg,
      onTap: () {
        tapPlaceHoldWidgetMethod(
          placeHoldType: placeHoldType,
        );
      },
    );
  }

  @override
  configleadingCallbak({
    BuildContext? context,
  }) {
    Navigator.pop(context!);
  }

  /// 脚手架背景颜色
  @override
  Color? configScallBackgroundColor() {
    return scallBackGroundColor;
  }

  /// 配置界面状态
  @override
  configPageState() {
    return pageState;
  }

  /// 是否使用脚手架
  @override
  bool configIsNeedScaffol() {
    return isNeedScaffol;
  }

  /// 是否显示导航栏
  @override
  bool configIsShowAppBar() {
    return isShowAppBar;
  }

  @override
  bool configIsNeedSafeArea() {
    return isNeedScaffol;
  }

  ///是否形变
  @override
  bool? configResizeToAvoidBottomInset() {
    return resizeToAvoidBottomInset;
  }

  /// 是否关闭顶部安全区域
  @override
  bool configSafeAreaTop() {
    return safeAreaTop;
  }

  /// 是否关闭底部安全区域
  @override
  bool configSafeAreaBottom() {
    return safeAreaBottom;
  }

  /// 配置导航栏标题
  @override
  String? createAppBarTitleStr() {
    return appBarTitle;
  }

  /// 配置导航栏背景颜色
  @override
  Color? createAppBarNavBackColor() {
    return navBackgroudColor;
  }

  /// =============== 网络事件 =============== ///

  /// =============== 触发事件 =============== ///
  ///

  /// 界面渲染完成
  void interfaceRenderingCompleted() {}

  /// 刷新界面
  configSetState(
    void Function() fn,
  ) {
    if (mounted) {
      setState(fn);
    }
  }

  /// 点击缺省页触发事件
  tapPlaceHoldWidgetMethod({
    required CommonPlaceHoldType placeHoldType,
  }) {}
}
